<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'
import { listRlzy } from "@/api/yljc/rlzy";


export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart',
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    }
  },
  data() {
    return {
      chart: null,
      MapList: [],
      rows: [],
      queryParams: {
        rlzyName: null,
        numPeople: null,
      },
      totalData: [],
    }
  },
  watch :{
    className: {
      deep: true,
      handler: function () {
        this.initChart()
        this.getList()
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
      this.getList()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    async getList() {
      var nameData = [];
      // var valueData = [];
      var totalData = [];
      this.loading = false;
      await listRlzy(this.queryParams).then(response => {
        this.MapList = response.rows;
        console.log("饼状图数据：" + this.MapList);    //有获取到后台数据,javaScript格式

    // forEach() 方法用于调用数组的每个元素，并将元素传递给回调函数。注意: forEach() 对于空数组是不会执行回调函数的。
    // push() 方法可向数组的末尾添加一个或多个元素，并返回新的长度。注意：新元素将添加在数组的末尾。此方法改变数组的长度。
        this.MapList.forEach(response => {
          nameData.push(response.rlzyName);
          // valueData.push(response.numPeople);
          totalData.push({
            value: response.numPeople, //将数组的numPeople值赋给value
            name: response.rlzyName, //将数组的rlzyName值赋给name
          });
        });
        // 动态插入数据时，数据在这里更新
        this.chart.setOption({
          legend: {
            data: nameData,
          },
          series: [{
            data: totalData
          }]
        });
      });
    },
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')

      this.chart.setOption({
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c} ({d}%)'
        },
        legend: {
          left: 'center',
          bottom: '10',
          data: [],
          // data: ['卫生技术人员', '管理人员', '后勤人员', '其他人员']
        },
        series: [
          {
            name: '在职人数',
            type: 'pie',
            roseType: 'radius',
            radius: [15, 95],
            center: ['50%', '38%'],
            data: [
              // { value: 1233, name: '卫生技术人员' },
              // { value: 133, name: '管理人员' },
              // { value: 123, name: '后勤人员' },
              // { value: 144, name: '其他人员' },
            ],
            animationEasing: 'cubicInOut',
            animationDuration: 2600
          }
        ]
      });
    },
  },
}
</script>
